package com.sdhs.paas.codegenerate.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdhs.paas.codegenerate.bean.Attribute;
import com.sdhs.paas.codegenerate.bean.CodeGenerateBean;
import com.sdhs.paas.codegenerate.bean.Column;
import com.sdhs.paas.codegenerate.bean.EntityParameter;
import com.sdhs.paas.codegenerate.bean.VueGenerateBean;
import com.sdhs.paas.codegenerate.service.GenerateCodeService;
import com.sdhs.paas.codegenerate.service.GenerateVueService;

/**
 * @author kjc
 * @version 1.0
 * @Title:  代码生成器
 * @date 2018/7/22/21:26
 */
public class VueGenerationUtils {

    /**
     * java代码生成接口
     * @param 模板名称
     * @return 返回生成成功的实体路径
     */
    public String createdVueCode(String templateName) {
    	String result = "success";
    	/**
    	 * 1.读取webTemplateconfig-vue.yml里面配置的信息
    	 * vuePath: 前端项目路径
		 * projectName: 代码目录
		 * gatewayPrefix: /api/test 网关的路由转发路径                       
		 * moduleName: 模块名称           
		 * author: 作者  
		 * listQuery: 查询条件
		 * columns:  temp的名称，表格的列名和form的表单信息                                 
		 *	 - columName: id                                 
		 *	   primaryKey: 0                                                          
		 *	 - columName: name
		 *	   primaryKey: 1
    	 * 
    	 * */
    	//读取yml的内容，并将值赋值给CodeGenerateBean对象
    	FreeMarkerConfigUtils config = new FreeMarkerConfigUtils();
    	VueGenerateBean vueGenerateBean = config.getVueYamlBean(templateName);
//    	System.out.println(vueGenerateBean.getAuthor());
//    	System.out.println(vueGenerateBean.getGatewayPrefix());
//    	System.out.println(vueGenerateBean.getListQuery());
//    	System.out.println(vueGenerateBean.getModuleName());
//    	System.out.println(vueGenerateBean.getProjectName());
//    	System.out.println(vueGenerateBean.getColumns().size());
        try {
        	//生成具体的java代码
            String entityURL = generateVueCode(vueGenerateBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 后台代码生成
     * @param entityParameter 前台接收到的实体
     * @return  成功 -----> 对应类的类路径，失败 ------> null
     */
    public String generateVueCode(VueGenerateBean vueGenerateBean){
    	//前端路径
    	String vuePath = vueGenerateBean.getVuePath();
    	//项目名
    	String projectName = vueGenerateBean.getProjectName();
//    	Utils.fristWordToUpperCase(vueGenerateBean.getClassName());
    	//网关前缀，为了api中的地址
    	String gatewayPrefix = vueGenerateBean.getGatewayPrefix();
    	//作者
    	String author = vueGenerateBean.getAuthor();
    	//模块名
    	String moduleName = vueGenerateBean.getModuleName();
    	//查询字段
    	String listQuery = vueGenerateBean.getListQuery();
    	//属性列
    	List<Column> columns = vueGenerateBean.getColumns();
    	//获取项目根路径
//    	String rootPath = this.getClass().getResource("/").getPath();
    	// E:\vue\isp-manager\src     \api    \views
    	//api生成的路径
    	String apiPath = vuePath+"\\src\\api\\"+projectName+"\\";
    	//前端页面的生成路径
    	String viewsPath = vuePath+"\\src\\views\\"+projectName+"\\"+moduleName.toLowerCase()+"\\";
    	//项目+\src\main\java\+包路径 = 组装成具体的路径
//    	List<Attribute> attr_list = new ArrayList<Attribute>();
//        for (Column column : vueGenerateBean.getColumns()){
//            attr_list.add(new Attribute(column.getColumName(), column.getPrimaryKey()));
//        }
//        String columnsToString = getColumnsToString(attr_list);
    	// 数据预处理
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("apiPath", apiPath);
        root.put("viewsPath", viewsPath);
        root.put("projectName", projectName);
        root.put("gatewayPrefix", gatewayPrefix);
        root.put("author", author);
        root.put("listQuery", listQuery);
        root.put("moduleName",moduleName);
        root.put("nativecolumns",columns);
        root.put("date", Utils.typeThree(new Date()));
        //生成实体类
        String result = GenerateVueService.generateApiJs(root);
        //生成dto
        GenerateVueService.generateViewsVue(root);
    	return "success";
    }
    public String generateJavaCode(EntityParameter entityParameter) {
        // 获取配置文件信息
        HashMap config = FreeMarkerConfigUtils.getConfig("templateConfig.yml");

        HashMap javaConfig = (HashMap) ((HashMap) config.get("generator")).get("java");

        HashMap javaCode = (HashMap) javaConfig.get("code");

        HashMap javaCodePackage = (HashMap) javaConfig.get("package");

        // 数据预处理
        Map<String, Object> root = new HashMap<String, Object>();
        String className = entityParameter.fristWordToUpperCase(entityParameter.getEntityName());
        root.put("entityPackageName", javaCodePackage.get("entityPackage").toString());
        root.put("servicePackageName", javaCodePackage.get("servicePackage").toString());
        root.put("serviceImplPackageName",javaCodePackage.get("serviceImplPackage").toString());
        root.put("controllerPackageName",javaCodePackage.get("controllerPackage").toString());
        root.put("mapperPackageName",javaCodePackage.get("mapperPackage").toString());
        root.put("className", className);
        root.put("author", "官江涛");
        root.put("date", Utils.typeThree(new Date()));
        List<Attribute> attr_list = new ArrayList<Attribute>();
        for (Column column : entityParameter.getColumns()){
            attr_list.add(new Attribute(column.getColumName(), column.getColumType()));
        }
        String entityURL = "success";
//        String columnsToString = getColumnsToString(attr_list);
//        root.put("attrs", attr_list);
//        root.put("columns",columnsToString);
//        // 生成Entity
//        String entityURL = generatorBackEndService.generateEntity(root,javaCode.get("entityPath").toString());
//        // 生成service代码
//        generatorBackEndService.generateService(root,javaCode.get("servicePath").toString());
//        // 生成Controller代码
//        generatorBackEndService.generateController(root,javaCode.get("controllerPath").toString());
//        //  生成serviceImpl代码
//        generatorBackEndService.generateServiceImpl(root,javaCode.get("serviceImplPath").toString());
//        //  生成javaMapper代码
//        generatorBackEndService.generateJavaMapper(root,javaCode.get("mapperPath").toString());
//        // 生成XMLMapper代码
//        generatorBackEndService.generateXMLMapper(root,javaCode.get("xmlPath").toString());
        return entityURL;
    }
    
    public static void main(String[] args) {
//    	FreeMarkerConfigUtils config = new FreeMarkerConfigUtils();
//    	CodeGenerateBean codeGenerateBean = config.getYamlBean("templateConfig.yml");
//    	System.out.println(codeGenerateBean.getAuthor());
//    	System.out.println(codeGenerateBean.getClassName());
//    	System.out.println(codeGenerateBean.getPackageName());
//    	System.out.println(codeGenerateBean.getColumns().size());
    	VueGenerationUtils vueGenerationUtils = new VueGenerationUtils();
    	vueGenerationUtils.createdVueCode("webTemplateconfig-vue.yml");
//    	String packageName = "com.sdhs.paas.codegenerate";
    	//com\sdhs\paas\codegenerate
//    	System.out.println(packageName.replace(".", "\\"));

	}
}
